package com.woniuxy.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.entity.Housing;
import com.woniuxy.entity.Owner;
import com.woniuxy.mapper.HousingMapper;
import com.woniuxy.mapper.OwnerMapper;
import com.woniuxy.service.HousingService;
import com.woniuxy.util.PageData;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@AllArgsConstructor
public class HousingServiceImpl implements HousingService {
    private HousingMapper housingMapper;
    private OwnerMapper ownerMapper;
    @Override
    public List<Housing> queryHouse() {
        return housingMapper.queryHouse();
    }

    @Override
    public PageInfo<Housing> queryHouses(PageData<Housing> pageData) {
        // 开始分页
        PageHelper.startPage(pageData.getPageNum(),pageData.getPageSize());
        List<Housing> housings = housingMapper.queryHouses(pageData.getParam());
        PageInfo pageInfo = new PageInfo(housings);
        return pageInfo;
    }

    @Override
    public void addHouse(Housing housing) {
        housingMapper.addHouse(housing);
    }

    @Override
    public boolean batchDelHouse(List<Integer> ids) {
        for (Integer id : ids) {
            Housing housing = housingMapper.queryHouseById(id);
            if(housing.getState()==1){
                return false;
            }
        }
        return housingMapper.batchDelHouse(ids);
    }

    @Override
    public Housing queryHouseById(Integer id) {
        return housingMapper.queryHouseById(id);
    }

    @Override
    public void saveUpdateHouse(Housing housing) {
        housingMapper.saveUpdateHouse(housing);
        ownerMapper.updateReamName(housing.getRealName(),housing.getOwnerId());
    }

    @Override
    public boolean delHouse(Integer id) {
        Housing housing = housingMapper.queryHouseById(id);
        if(housing.getState()==0){
            housingMapper.delHouse(id);
            return true;
        }else {
            return false;
        }

    }
}
